﻿// Decompiled with JetBrains decompiler
// Type: Unity.Netick.CodeGen.NetworkBehaviourILPP
// Assembly: Unity.Netick.CodeGen, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8B3B8749-9F65-48DA-BE6F-84634E89AC85
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Editor\Unity.Netick.CodeGen.dll

using Mono.Cecil;
using Mono.Cecil.Cil;
using Netick.CodeGen;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.CompilationPipeline.Common.Diagnostics;
using Unity.CompilationPipeline.Common.ILPostProcessing;

#nullable disable
namespace Unity.Netick.CodeGen;

public sealed class NetworkBehaviourILPP : ILPostProcessor
{
  private readonly List<DiagnosticMessage> m_Diagnostics = new List<DiagnosticMessage>();

  public virtual ILPostProcessor GetInstance() => (ILPostProcessor) this;

  public virtual bool WillProcess(ICompiledAssembly compiledAssembly)
  {
    return ((IEnumerable<string>) compiledAssembly.References).Any<string>((Func<string, bool>) (filePath => Path.GetFileNameWithoutExtension(filePath) == "Netick"));
  }

  public virtual ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
  {
    AssemblyDefinition assemblyDefinition = CodeGenUtils.AssemblyDefinitionFor(compiledAssembly, out PostProcessorAssemblyResolver _);
    if (assemblyDefinition == null)
    {
      this.m_Diagnostics.AddError("Cannot read Netick Runtime assembly definition: " + compiledAssembly.Name);
      return (ILPostProcessResult) null;
    }
    if (!this._Process(assemblyDefinition))
      return (ILPostProcessResult) null;
    MemoryStream memoryStream1 = new MemoryStream();
    MemoryStream memoryStream2 = new MemoryStream();
    WriterParameters writerParameters = new WriterParameters()
    {
      SymbolWriterProvider = (ISymbolWriterProvider) new PortablePdbWriterProvider(),
      SymbolStream = (Stream) memoryStream2,
      WriteSymbols = true
    };
    assemblyDefinition.Write((Stream) memoryStream1, writerParameters);
    return new ILPostProcessResult(new InMemoryAssembly(memoryStream1.ToArray(), memoryStream2.ToArray()), this.m_Diagnostics);
  }

  public bool _Process(AssemblyDefinition assemblyDefinition)
  {
    ModuleDefinition mainModule = assemblyDefinition.MainModule;
    string str = ((ModuleReference) assemblyDefinition.MainModule).Name.Replace(".dll", "");
    bool flag = false;
    if (str == "Netick" || str == "Netick.Unity" || ((IEnumerable<AssemblyNameReference>) assemblyDefinition.MainModule.AssemblyReferences).FirstOrDefault<AssemblyNameReference>((Func<AssemblyNameReference, bool>) (x => x.Name.Contains("Netick"))) == null)
      return false;
    if (mainModule != null)
    {
      NetickILProcessor netickIlProcessor = new NetickILProcessor();
      UnityCodeGen engineWeaver = new UnityCodeGen();
      engineWeaver.Init(mainModule, this.m_Diagnostics);
      netickIlProcessor.Init((ICodeGenUser) engineWeaver, mainModule);
      flag = netickIlProcessor.ProcessAssembly(assemblyDefinition);
    }
    else
      this.m_Diagnostics.AddError($"Cannot get main module from assembly definition: {assemblyDefinition.Name}");
    return flag;
  }
}
